<?php
class User_childer extends MY_Controller
{	
	function __construct() {
        parent::__construct();
    }

    function index(){
        $data = $this->user_post_data;
        if(!empty($data)){
            // $data = json_decode($data,true);
            $this->load->model("Users");
            $limit = !empty($data['limit']) ? $data['limit'] : 0;
            $result = $this->Users->get_user_childer_list($limit * $this->offset, $this->offset, array("user_id" => $data['user_token']['user_id']));
            /*$data_obj = array(
                'count' => $result['count'],
                'list' => $result['list'],
             );*/
            $result['page_total'] = ceil($result['count'] / $this->offset);
            echo json_encode(array("code" => 1, "data" => $result));   // 
        }else{
            echo json_encode(array("code" => 404, "user" => 'not user'));
        }
        
    }

    function edit(){
        $data = $this->user_post_data;
        if(!empty($data)){
            // $data = json_decode($data,true);
            $childer = array(
                'nick' => $data['user_name'], 
                'birthday' => $data['birthday'], 
                'school_year' => $data['school_year']."-01", 
                'gender' => $data['gender'], 
                );

            $this->db->update('tiku_user_childrens',$childer,array('id' => $data['childer_id']));
           
           
            echo json_encode(array("code" => 1, "data" => null,"msg"=>"success"));   // 
             // echo $userid;
        }else{
            echo json_encode(array("code" => 404, "user" => 'not user'));
        }
    }   


    function add(){
        $data = $this->user_post_data;
    	if(!empty($data)){
            $childer = array(
                'user_id' => $data['user_token']['user_id'], 
                'nick' => $data['user_name'], 
                'grade' => $data['grade'], 
                'gender' => $data['gender'], 
                'stars' => 0, 
                'school_id' => 0, 
                'add_time' => time(), 
                );
            $this->db->insert('tiku_user_childrens',$childer);
            $child_id = $this->db->insert_id();
            $this->load->model("Tasks");
            $reg_push_task_list = $this->Tasks->get_reg_push_list();
            $child_task = array();
            foreach ($reg_push_task_list as $key => $value) {
                $task_data = array("user_id" => $data['user_token']['user_id'], "child_id" => $child_id, "name" => $value['name'], "task_type_id" => $value['id'], "describe" => $value['remark'], "repeat" => $value['repeat'], "task_time" => $value['default_time'],"time_long"=>$value['time_long']);
                if(!empty($value['default_time'])){
                    $task_time = explode(":", $value['default_time']);
                    $task_data['remind_hour'] = $task_time[0];
                    $task_data['remind_min'] = $task_time[1];
                }
                $child_task[] = $task_data;
            }
            if(count($child_task) > 0){
                $this->db->insert_batch("tasks", $child_task);
            }

            $user_info = get_field_by_where("users", "child_id", "id='{$data['user_token']['user_id']}'");
            if($user_info['child_id'] <= 0){
                $this->db->update("users", array("child_id" => $child_id), array("id" => $data['user_token']['user_id']));
                echo json_encode(array("code" => 1, "child_id" => $child_id));
            }else{
                echo json_encode(array("code" => 1));
            }
    	}else{
            echo json_encode(array("code" => 404, "user" => 'not user'));
        }
    }

    function del(){
        $data = $this->user_post_data;
        if(!empty($data)){
            // $data = json_decode($data,true);
            $childer_id = $data['id'];
            $sql = "DELETE FROM tiku_user_childrens WHERE id={$childer_id}";
            $this->db->query($sql);
           
            echo json_encode(array("code" => 1, "data" => null));   // 
             // echo $userid;
        }else{
            echo json_encode(array("code" => 404, "user" => 'not user'));
        }
    }

    function get(){
        $data = $this->user_post_data;
        if(!empty($data)){
            // $data = json_decode($data,true);
            $childer_id = $data['id'];
            $sql = "SELECT * FROM tiku_user_childrens WHERE id={$childer_id}";
            $result = $this->db->query($sql)->row_array();
           
            echo json_encode(array("code" => 1, "data" => $result));   // 
             // echo $userid;
        }else{
            echo json_encode(array("code" => 404, "user" => 'not user'));
        }
    }

    function update_user_child_id(){
        $data = $this->user_post_data;
        $child_info = get_field_by_where("user_childrens", "*", "id='{$data['child_id']}'");
        if(!$child_info){
            echo json_encode(array("code" => 0, "msg" => 'not childer'));
            exit;
        }
        if($child_info['user_id'] != $data['user_token']['user_id']){
            echo json_encode(array("code" => 0, "msg" => '无法切换孩子'));
            exit;
        }
        $this->db->update("users", array("child_id" => $data['child_id']), array("id" => $data['user_token']['user_id']));
        echo json_encode(array("code" => 1, "msg" => '切换成功'));
        exit;
    }

    function get_child_stars(){
        $data = $this->user_post_data;
        $today_time = strtotime(date('Y-m-d', time()));
        $child_info = get_field_by_where("user_childrens", "stars,used_stars", "id='{$data['child_id']}'");
        $today_stars_info = get_field_by_where("user_children_today_stars", "stars", "child_id='{$data['child_id']}' AND last_time >= $today_time");
        $today_stars = !empty($today_stars_info['stars']) ? $today_stars_info['stars'] : 0;
        $stars_data['total_stars'] = !empty($child_info['stars']) ? $child_info['stars'] : 0;
        $stars_data['today_stars'] = $today_stars;
        $stars_data['sy_stars'] = $child_info['stars'] - $child_info['used_stars'];
        echo json_encode(array("code" => 1, "stars_data" => $stars_data));
    }
}